package com.wanmait.baohan.controller.businessTripController;


import com.github.pagehelper.PageInfo;
import com.wanmait.baohan.pojo.*;
import com.wanmait.baohan.service.*;
import com.wanmait.baohan.vo.DataVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * TODO
 *
 * @author hxn
 * @version 1.0
 * @date 2020/12/1 11:06
 * @description:
 */
@Controller
public class BusinessTripController {
    @Resource
    private TravelApplicationService travelApplicationService;
    @Resource
    private StaffService staffService;
    @Resource
    private SuppilerService suppilerService;
    @Resource
    private TravelApprovalService travelApprovalService;
    @Resource
    private  TravelLogService travelLogService;
    @Resource
    private  TravelReportServce travelReportServce;
    /**
     * 新建出差申请页面
     * hxn
     */
    @RequestMapping("business-trip/application")
    public String application(Model model){
        model.addAttribute("staffs", staffService.findAll());
        model.addAttribute("suppilers",suppilerService.findAll());
        return "business-trip/application";
    }

    /**
     * 我的出差申请列表
     * hxn
     */
    @RequestMapping("business-trip/table-applications")
    public String tableApplications(){
        return "business-trip/table-applications";

    }

    /**
     * 提交出差申请
     * 提交出差人员
     * hxn
     */
    @RequestMapping("submitApplication")
    @ResponseBody
    public void submitApplication(TravelApplication travelApplication){
        travelApplicationService.submitApplication(travelApplication);
    }

    /**
     * 获取我的所有出差申请 分页
     * 条件查询
     * hxn
     */
    @RequestMapping("getMyApplications")
    @ResponseBody
    public DataVo<TravelApplication> getMyApplications(Date beginDate, Date endDate, Integer page, Integer limit, String supplierName, Integer node, String planTravelDate, HttpServletRequest request){
        //创建查询条件对象
        TravelApplication travelApplication = new TravelApplication();
        Supplier supplier = new Supplier();
        supplier.setName("".equals(supplierName)?null:supplierName);
        //supplier.setName(supplierName);
        ProcessNode processNode = new ProcessNode();
        processNode.setNode(node);
        travelApplication.setProcessNode(processNode);
        travelApplication.setSupplier(supplier);
        Staff staff = (Staff) request.getSession().getAttribute("user");
        travelApplication.setStaff(staff);
        travelApplication.setPlanTravelDate("".equals(planTravelDate)?null:new Date());
        //封装到vo
        DataVo<TravelApplication> dataVo = new DataVo<>();
        PageInfo<TravelApplication> travelApplicationPageInfo = travelApplicationService.selectMyApplication(page, limit, travelApplication, beginDate, endDate);
        dataVo.setData(travelApplicationPageInfo.getList());
        //getTotal查出的总记录数
        dataVo.setCount(travelApplicationPageInfo.getTotal());
        return dataVo;
    }
    /**
     * 打开出差详情页面
     * hxn
     */
    @RequestMapping("business-trip/applicationDetails")
    public String applicationsDetails(Model model, Integer id,Integer node){
        model.addAttribute("travelApplication",travelApplicationService.findById(id));
        model.addAttribute("TravelApprovel",travelApprovalService.findApprovalToApplicationById(id));
        model.addAttribute("log", travelLogService.selectByTravelApplitionId(id));
        TravelReport travelReport = travelReportServce.selectByPrimaryKeyById(id);
        if (travelReport == null){
            travelReport = new TravelReport();
            travelReport.setReport("");
        }
        model.addAttribute("report",travelReport );

        return "business-trip/applicationDetails";
    }

    /**
     * 驳回申请
     * hxn
     */
    @RequestMapping("businesstrip/rejectApplication")
    public void rejectApplication(Integer id, String value){
        travelApplicationService.rejectApplication(id, value);
    }
}
